<?php

namespace App\Http\Controllers\Web\Member\Seller;

use App\Http\Controllers\Web\BaseController;
use Illuminate\Http\Request;

class ShopEmployeeController extends BaseController
{
    protected $api_url = [
        'index' => ['get', 'seller/member'],
        'auth'  => ['get', 'seller/member/auth'],
        'show'  => ['get', 'seller/member/%d'],
        'store' => ['post', 'seller/member'],
        'update'=> ['put', 'seller/member/%d'],
        'checkEmp'=> ['get', 'seller/member/checkEmp/%s' ],
        'nav'   => ['get', 'seller/member/nav/%d'],
        'destroy'=>['delete', 'seller/member/%d'],

        'verifycode' => ['get', 'verifycode'],
        'checkphone' => ['get', 'user/checkphone'],
    ];

    protected $api_host;

    protected $request;

    public function __construct(Request $request)
    {
        $this->request = $request;
        $this->api_host = config('url.api_web');
    }

    public function index()
    {
        //请求部分
        $nav= http_web($this->api_url['nav'],[session('user_id')])->send();

        $ret= http_web($this->api_url['index'])->send(['page' => $this->request->input('page',1)]);
        //结果处理部分
        $data = paginate($this->request, $ret['data']);
        $data['nav']=$nav['data'];
        return view('web.user.seller.member',$data);

    }

    public function show($id)
    {
        //请求部分
        $ret = http_web($this->api_url['show'], [$id])->send();
        $auth = http_web($this->api_url['auth'])->send();

        $data=[
            'ret'=>$ret['data'],
            'hasAction'=>array_column($ret['data']['shop_right_auth'],'resource_id'),
            'auth'=>$auth['data'],
            'type'=>'put'
        ];
        //dd($data);
        return view('web.user.seller.member_edit',$data);

    }
    /**
     *  第一步 输入手机号
     **/
    public function create(){
        return view('web.user.seller.member_add' , ['type' => 'create']);
    }
    /**
     *  第二步 获取验证码
     **/
    public function create2(){

        $data  = $this->request->all();
        $phone = $data['phone'];
        // 验证手机号格式
        if (!preg_match('/^1[345789][0-9]{9}$/',$phone,$matches)){
             return redirect()->back();
        }

        return view('web.user.seller.member_add2', ['type' => 'create','phone'=>$phone]);
    }
    /**
     *  第三步 生成数据
     **/
    public function create3(){
        $data  = $this->request->all();

        $this->validate($this->request, [
            'phone' => 'required',
            'code' => 'required',
        ]);

        $phone = $data['phone'];
        $captcha = $data['code'];
        $param = ['phone' => $phone,'code' => $captcha];
        // 是否已添加为本店铺会员
        $ret = http_web($this->api_url['checkEmp'],[$phone])->send($param);

        if (isset($ret['code']) && $ret['code']===0){

            $ret = http_web($this->api_url['store'])->send($param);

            return view('web.user.seller.member_add3', ['type' => 'create','data'=>$ret['data']]);

        }else{
            return redirect('/member/seller/member');
        }

    }
    /**
     *  检测手机号是否是已注册会员
     **/
    public function checkphone(){

        $data  = $this->request->all();
        $phone = $data['phone'];

        //验证是否注册
        $param = ['phone' => $phone];
        $ret = http_web($this->api_url['checkphone'])->send($param);

        if(isset($ret['data']) && $ret['data']===902 ){
            // 是否已添加为本店铺会员
            $ret = http_web($this->api_url['checkEmp'],[$phone])->send($param);

            if (isset($ret['code']) && $ret['code']===0){
                return response()->json(['code'=>0,'message'=>'']);
            }else{
                return response()->json(['code'=>1,'message'=>$ret['message']]);
            }

        }else{
            return response()->json(['code'=>1,'message'=>$ret['message']]);
        }
    }
    /**
     *  检测验证码
     **/
    public function verifyCode(){

        $data  = $this->request->all();
        $phone = $data['phone'];
        $captcha = $data['code'];
        //验证是否注册
        $param = ['phone' => $phone,'code' => $captcha];
        $ret = http_web($this->api_url['verifycode'])->send($param);

        return $ret;
    }

    public function edit($id)
    {
        //请求部分
        $ret = http_web($this->api_url['show'], [$id])->send();
        $auth = http_web($this->api_url['auth'])->send();

        $data=[
            'ret'=>$ret['data'],
            'auth'=>$auth['data'],
            'type'=>'put'
        ];
        return view('web.user.seller.member_edit',$data);
    }

    public function update($id)
    {
        $ret = http_web($this->api_url['update'], [$id])->send($this->request->all());

        if(isset($ret['code']) && $ret['code']===0 ){
            return response()->json(['code'=>0,'message'=>'']);
        }else{
            return response()->json(['code'=>1,'message'=>$ret['message']]);
        }

    }

    public function destroy($id)
    {
        $ret = http_web($this->api_url['destroy'], [$id])->send();
        if(isset($ret['code']) && $ret['code']==0 ){
           return response()->json(['code'=>0,'message'=>'删除成功']);
        }else{
           return response()->json(['code'=>1,'message'=>$ret['message']]);
        }
    }
}